﻿using System;
using System.Collections.Generic;
using AngryBullets;
using AngryBullets.Sprite;
using AngryBullets.Sprite.Object;
using System.Drawing;
using Microsoft.Xna.Framework;
using System.IO;

namespace AngryBullets
{
    class Map
    {
        #region Fields

        private int maxX, maxY;
        static private List<Sprite.Sprite> _mapObjects = new List<Sprite.Sprite>();
        static private List<AnimationSprite> _mapAnime = new List<AnimationSprite>();

        #endregion

        #region Propertie

        static public List<Sprite.Sprite> MapObjects
        {
            get { return _mapObjects; }
            set { _mapObjects = value; }
        }

        static public List<AnimationSprite> MapAnime
        {
            get { return _mapAnime; }
            set { _mapAnime = value; }
        }

        #endregion

        #region Initialisation

        public Map(string file)
        {
            CreateData(file);
        }

        #endregion

        #region Methods

        private void CreateData(string file)
        {
            FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);

            char[,] map;

            maxX = Convert.ToInt32(sr.ReadLine());
            maxY = Convert.ToInt32(sr.ReadLine());

            map = new char[maxX, maxY];
            _mapObjects.Add(new Background());

            for (int i = 0; i < map.GetLength(0); i++)
            {
                for (int j = 0; j < map.GetLength(1); j++)
                    map[i, j] = Convert.ToChar(sr.Read());
            }

            for (int i = 0; i < map.GetLength(0); i++)
            {
                for (int j = 0; j < map.GetLength(1); j++)
                    SelectObject(i, j, map);
            }

            sr.Close();
            fs.Close();
        }

        private void SelectObject(int i, int j, char[,] map)
        {
            if (map[i, j] != '.')
            {
                if (CheckPos(i, j, map, 'o'))
                    _mapAnime.Add(new Baliste(new Vector2(i, j)));
                else if (CheckPos(i, j, map, '|'))
                    _mapObjects.Add(new Tower(new Vector2(i, j)));
                else if (CheckPos(i, j, map, '_'))
                {
                    if (!CheckPos(i, j - 1, map, '_'))
                    {
                        _mapObjects.Add(new Sprite.Floor(new Vector2(i, j)));
                        for (int k = j; k < maxY; k++)
                            map[i, k] = '_';
                    }
                }
            }
        }

        private bool CheckPos(int i, int j, char[,] map, char k)
        {
            if (i > 0 && j > 0)
                return (map[i, j] == k && map[i - 1, j] != k && map[i, j - 1] != k);
            else if (i > 0)
                return (map[i, j] == k && map[i - 1, j] != k);
            else if (j > 0)
                return (map[i, j] == k && map[i, j - 1] != k);
            else if (i == 0 && j == 0)
                return (map[i, j] == k);

            return false;
        }

        #endregion
    }
}
